package com.zl.tby.screen.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.dto.TbyEcologyInfoQuery;
import com.zl.tby.model.TbyEcologyInfo;
import com.zl.tby.repository.TbyEcologyInfoRepo;
import com.zl.tby.screen.enums.TbyEcologyColumnEnum;
import com.zl.tby.screen.enums.TbyEcologyColumnMaxminEnum;
import com.zl.tby.screen.enums.TbyEcologyXAxisEnum;
import com.zl.tby.screen.service.ITbyEcologyInfoScreenService;
import com.zl.tby.screen.vo.ecology.ChartsData;
import com.zl.tby.screen.vo.ecology.TbyEcologyScreenInfo;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.wood.DbContext;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 生态因子大屏数据
 * @author changpeng
 * @version 1.0
 * @since 2024/11/6 13:25
 */
@Component
public class TbyEcologyInfoScreenServiceImpl implements ITbyEcologyInfoScreenService {

    @Db
    private DbContext db;
    @Inject
    private TbyEcologyInfoRepo tbyEcologyInfoRepo;

    @Override
    public List<ChartsData> statisticChartData(TbyEcologyInfoQuery query) {
        Date start = ToolsUtil.dateString2Util(query.getStartDate() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date end = ToolsUtil.dateString2Util(query.getEndDate() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        long startTime = start.getTime();
        long endTime = end.getTime();
        long day = (endTime - startTime) / (1000 * 60 * 60 * 24);
        query.setStart(start);
        query.setEnd(end);
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        query.setDay(day);
        // 根据时间范围生成x轴的坐标
        TbyEcologyXAxisEnum xAxisEnum = TbyEcologyXAxisEnum.YEAR;
        if (startTime == endTime) {
            xAxisEnum = TbyEcologyXAxisEnum.HOUR;
        } else if (day <= 31) {
            xAxisEnum = TbyEcologyXAxisEnum.DAY;
        } else if (day > 31 && day <= 366) {
            xAxisEnum = TbyEcologyXAxisEnum.MONTH;
        }
        // 查询并转化数据
        return TbyEcologyColumnEnum.getByCode(query.getColumn()).search(db, xAxisEnum, query);
    }

    /**
     * 生态检测数据图表接口原来只返回平均值，现在增加最高值和最低值2025年10月15日 09:57:15bjs
     * @param query
     * @return
     */
    @Override
    public List<ChartsData> statisticChartDataMaxmin(TbyEcologyInfoQuery query) {
        Date start = ToolsUtil.dateString2Util(query.getStartDate() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date end = ToolsUtil.dateString2Util(query.getEndDate() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        long startTime = start.getTime();
        long endTime = end.getTime();
        long day = (endTime - startTime) / (1000 * 60 * 60 * 24);
        query.setStart(start);
        query.setEnd(end);
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        query.setDay(day);
        // 根据时间范围生成x轴的坐标
        TbyEcologyXAxisEnum xAxisEnum = TbyEcologyXAxisEnum.YEAR;
        if (startTime == endTime) {
            xAxisEnum = TbyEcologyXAxisEnum.HOUR;
        } else if (day <= 31) {
            xAxisEnum = TbyEcologyXAxisEnum.DAY;
        } else if (day > 31 && day <= 366) {
            xAxisEnum = TbyEcologyXAxisEnum.MONTH;
        }
        // 查询并转化数据
        return TbyEcologyColumnMaxminEnum.getByCode(query.getColumn()).search(db, xAxisEnum, query);
    }
    @Override
    public TbyPageResult<TbyEcologyInfo> historyList(TbyEcologyInfoQuery query) {
        IPage<TbyEcologyInfo> iPage = tbyEcologyInfoRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue().andIf(StringUtils.isNotEmpty(query.getStartDate()), "report_date >= ?", query.getStartDate() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getEndDate()), "report_date <= ?", query.getEndDate() + " 23:59:59")
                    .andIf(query.getStationId() != null, "station_id = ?", query.getStationId())
                    .orderBy("report_date desc");
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public TbyEcologyScreenInfo detailByStationId(Long stationId) {
        try {
            List<TbyEcologyScreenInfo> dataList = db.table("tby_ecology_info").whereTrue().andIf(true, "report_date >= ?", ToolsUtil.dateUtil2String(new Date(), "yyyy-MM-dd") + " 00:00:00")
                    .andIf(true, "report_date <= ?", ToolsUtil.dateUtil2String(new Date(), "yyyy-MM-dd") + " 23:59:59")
                    .andIf(stationId != null, "station_id = ?", stationId).orderBy("report_date desc")
                    .selectList("*", TbyEcologyScreenInfo.class);
            if (dataList != null && dataList.size() > 0) {
                TbyEcologyScreenInfo entity = dataList.get(0);
                /*if (entity.getLux() != null) {
                    entity.setLux(new BigDecimal(entity.getLux()).multiply(new BigDecimal(126.58)).intValue());
                }*/
                if (entity.getWindDirection() != null) {
                    entity.setWindDirectionText(ToolsUtil.windDirectionSwitch((float) entity.getWindDirection()));
                }
                return entity;
            }
            return null;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
